#include "logtools.h"
#include "ui_logtools.h"

bool fileExist;
bool fileEmpty;

Logtools::Logtools(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Logtools)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("日志分析管理工具"));

    //设置主窗口背景色
    QPalette mainPalette;
    mainPalette.setColor(QPalette::Window, QColor(255, 255, 255));
    this->setPalette(mainPalette);
    //设置主菜单
    logtoolsSetMainMenu();
    //设置过滤器
    logtoolsSetFilterBar();
    //设置表格视图
    logtoolsSetTableView();
    this->model = new QStandardItemModel(this);
    //菜单点击事件
    logtoolsMenuClickedEvens();
    //获取当前用户的桌面路径
    logtoolsGetHomePath();
    //初始化表格显示第一个菜单项
    logtoolsSwitchSlots();
}

Logtools::~Logtools()
{
    delete ui;
}

void Logtools::logtoolsAboutLogtools()
{
    QDialog *dialogAbout = new QDialog(this);
    dialogAbout->setFixedSize(518, 290);
    dialogAbout->setWindowTitle("关于");

    QPalette aboutPalette;
    aboutPalette.setColor(QPalette::Window, QColor(255, 255, 255));
    dialogAbout->setPalette(aboutPalette);

    QLabel *softName = new QLabel(tr("日志分析管理工具"), dialogAbout);
    softName->move(36, 30);
    QFont fontName("Noto Sans CJK SC", 12);
    fontName.setBold(true);
    softName->setFont(fontName);
    
    QProcess process;
    QStringList params;
    process.start("bash", QStringList() << "-c" << "dpkg -l | grep nfs-logtools");
    process.waitForFinished();
    QString appVersion = process.readAllStandardOutput();
    ParserLog::formatString(appVersion);
    QLabel *softVersion = new QLabel(appVersion.split(" ")[2],dialogAbout);
    softVersion->move(36, 66);

    QLabel *softIcon = new QLabel(dialogAbout);
    softIcon->move(406, 36);
    softIcon->setPixmap(QPixmap(":/icon/nfs-logtools.png"));

    QFrame *line = new QFrame(dialogAbout);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Plain);
    line->setFixedWidth(446);
    line->setStyleSheet("QFrame {color:#EBEBEB}");
    line->move(36, 130);

    QLabel *softInfo = new QLabel(tr("日志分析管理工具是一款支持系统、应用等相关日志信息查看、统计、分析、\n导出的管理工具。"), dialogAbout);
    softInfo->move(36, 160);

    QLabel *softCopyrite = new QLabel(tr("© 2022 中科方德软件有限公司"), dialogAbout);
    softCopyrite->move(36, 246);

    dialogAbout->exec();
}

void Logtools::logtoolsSetMainMenu()
{
    this->menuBar = new QMenuBar;
    this->setMenuBar(menuBar);
//    ui->menubar->setMinimumHeight(26);

    this->menuBar->setStyleSheet("QMenuBar::item:pressed {background-color:#3584E4; color: white}");

    this->menuView = new QMenu(tr("日志查看(V)"));
    this->menuAnalyze = new QMenu(tr("日志分析(A)"));
    this->menuLevel = new QMenu(tr("日志分级(L)"));
    this->menuCollect = new QMenu(tr("日志一键采集(C)"));
    this->menuHelp = new QMenu(tr("帮助(H)"));
    this->menuBar->addMenu(menuView);
    this->menuBar->addMenu(menuAnalyze);
    this->menuBar->addMenu(menuLevel);
    this->menuBar->addMenu(menuCollect);
    this->menuBar->addMenu(menuHelp);
    this->menuView->setStyleSheet("QMenu:item {background-color:#3584E4}");
    this->menuAnalyze->setStyleSheet("QMenu:item {background-color:#3584E4}");
    this->menuLevel->setStyleSheet("QMenu:item {background-color:#3584E4}");
    this->menuCollect->setStyleSheet("QMenu:item {background-color:#3584E4}");
    this->menuHelp->setStyleSheet("QMenu:item {background-color:#3584E4}");

    this->actSwitch = new QAction(tr("开关机与登录情况"), this);
    this->actSysInfo = new QAction(tr("系统相关信息"), this);
    this->actSysBoot = new QAction(tr("系统启动情况"), this);
    this->actPackage = new QAction(tr("包安装情况"), this);
    this->actKernel = new QAction(tr("内核信息"), this);
    this->actSoftwareUpdate = new QAction(tr("软件更新情况"), this);
    this->actPrint = new QAction(tr("打印信息"), this);
    this->actBurn = new QAction(tr("刻录信息"), this);
    this->actAudit = new QAction(tr("审计日志"), this);
    this->actSession = new QAction(tr("会话日志"), this);
    this->actXorg = new QAction(tr("Xorg信息"), this);
    this->actApplication = new QAction(tr("应用日志"), this);
    this->actSwitchTimes = new QAction(tr("开关机次数统计"), this);
    this->actDebug = new QAction(tr("调试信息"), this);
    this->actInfo = new QAction(tr("通知级别"), this);
    this->actNotice = new QAction(tr("注意级别"), this);
    this->actWarning = new QAction(tr("警告级别"), this);
    this->actError = new QAction(tr("错误级别"), this);
    this->actCritical = new QAction(tr("临界级别"), this);
    this->actAlert = new QAction(tr("警戒级别"), this);
    this->actEmerg = new QAction(tr("致命级别"), this);
    this->actSyslog = new QAction(tr("系统所有日志"), this);
    this->actPrintRelated = new QAction(tr("打印相关日志"), this);
    this->actAbout = new QAction(tr("关于"), this);
    this->menuView->addAction(actSwitch);
    this->menuView->addAction(actSysInfo);
    this->menuView->addAction(actSysBoot);
    this->menuView->addAction(actPackage);
    this->menuView->addAction(actKernel);
    this->menuView->addAction(actSoftwareUpdate);
    this->menuView->addAction(actPrint);
    this->menuView->addAction(actBurn);
    this->menuView->addAction(actAudit);
    this->menuView->addAction(actSession);
    this->menuView->addAction(actXorg);
    this->menuView->addAction(actApplication);
    this->menuAnalyze->addAction(actSwitchTimes);
    this->menuLevel->addAction(actDebug);
    this->menuLevel->addAction(actInfo);
    this->menuLevel->addAction(actNotice);
    this->menuLevel->addAction(actWarning);
    this->menuLevel->addAction(actError);
    this->menuLevel->addAction(actCritical);
    this->menuLevel->addAction(actAlert);
    this->menuLevel->addAction(actEmerg);
    this->menuCollect->addAction(actSyslog);
    this->menuCollect->addAction(actPrintRelated);
    this->menuHelp->addAction(actAbout);
}

void Logtools::logtoolsSetFilterBar()
{
    ui->labelInfo->setText("开关机与登录情况");
    ui->lineEditSearch->setPlaceholderText(tr("关键字"));
    ui->comboBoxApp->addItem("所有应用");
    ui->comboBoxApp->setStyleSheet("QComboBox {selection-background-color:#3584E4}");
#ifdef YEARMONTH_COMBOBOX
    ui->comboBoxYear->addItem("所有");
    ui->comboBoxMonth->addItem("所有");
    ui->comboBoxYear->setStyleSheet("QComboBox {selection-background-color:#3584E4}");
    ui->comboBoxMonth->setStyleSheet("QComboBox {selection-background-color:#3584E4}");

    //建立年份和月份的关联
    connect(ui->comboBoxYear, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),\
            this, &Logtools::on_ComboboxYear_Changed);
    connect(ui->comboBoxMonth, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),\
            this, &Logtools::on_ComboboxMonth_Changed);
#endif
#ifdef NEARLY_TIME
    logtoolsFilterSetNearlyButtonStyle();
    logtoolsFilterSetNearlyBxuttonGroup();
    logtoolsFilterClearNearlyButtonStatus();
#endif
}

void Logtools::logtoolsSetTableView()
{
    //设置选取整行
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    //设置不可编辑
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置选中行的背景颜色
    ui->tableView->setStyleSheet("QTableView::item{selection-background-color:#B2E1FF; selection-color:#000000;}");
    //设置列索引不可见
    ui->tableView->verticalHeader()->setVisible(false);
    //设置行铺满
    ui->tableView->horizontalHeader()->setStretchLastSection(true);
    //隐藏网格线
    ui->tableView->setShowGrid(false);
    //设置表头左对齐
    ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);
    //设置列默认宽度
    ui->tableView->horizontalHeader()->setDefaultSectionSize(90);
    //设置水平滚动条
//    ui->tableView->setAutoScroll(true);
//    ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
//    ui->tableView->horizontalScrollBar();
//    ui->tableView->horizontalScrollBarPolicy();
    //行色交替显示
    if(!ui->tableView->alternatingRowColors()) {
        ui->tableView->setAlternatingRowColors(true);
    }
}

void Logtools::logtoolsMenuClickedEvens()
{
//  开关机与登录情况
    connect(this->actSwitch, &QAction::triggered, this, &Logtools::logtoolsSwitchSlots);
//  系统相关信息
    connect(this->actSysInfo, &QAction::triggered, this, &Logtools::logtoolsSysInfoSlots);
//  系统启动信息
    connect(this->actSysBoot, &QAction::triggered, this, &Logtools::logtoolsSysBootSlots);
//  包安装情况
    connect(this->actPackage, &QAction::triggered, this, &Logtools::logtoolsPackageSlots);
//  内核信息
    connect(this->actKernel, &QAction::triggered, this, &Logtools::logtoolsKernelSlots);
//  软件更新信息
    connect(this->actSoftwareUpdate, &QAction::triggered, this, &Logtools::logtoolsSoftwareUpdateSlots);
//  打印信息
    connect(this->actPrint, &QAction::triggered, this, &Logtools::logtoolsPrintSlots);
//  刻录日志
    connect(this->actBurn, &QAction::triggered, this, &Logtools::logtoolsBurnSlots);
//  审计日志
    connect(this->actAudit, &QAction::triggered, this, &Logtools::logtoolsAuditSlots);
//  会话日志
    connect(this->actSession, &QAction::triggered, this, &Logtools::logtoolsSessionSlots);
//  Xorg信息
    connect(this->actXorg, &QAction::triggered, this, &Logtools::logtoolsXorgSlots);
//  应用日志
    connect(this->actApplication, &QAction::triggered, this, &Logtools::logtoolsApplicationSlots);
//  开关机次数统计
    connect(this->actSwitchTimes, &QAction::triggered, this, &Logtools::logtoolsSwitchTimesSlots);
//  调试级别
    connect(this->actDebug, &QAction::triggered, this, &Logtools::logtoolsDebugSlots);
//  通知级别
    connect(this->actInfo, &QAction::triggered, this, &Logtools::logtoolsInfoSlots);
//  注意级别
    connect(this->actNotice, &QAction::triggered, this, &Logtools::logtoolsNoticeSlots);
//  警告级别
    connect(this->actWarning, &QAction::triggered, this, &Logtools::logtoolsWarningSlots);
//  错误级别
    connect(this->actError, &QAction::triggered, this, &Logtools::logtoolsErrorSlots);
//  临界级别
    connect(this->actCritical, &QAction::triggered, this, &Logtools::logtoolsCriticalSlots);
//  警戒级别
    connect(this->actAlert, &QAction::triggered, this, &Logtools::logtoolsAlertSlots);
//  致命级别
    connect(this->actEmerg, &QAction::triggered, this, &Logtools::logtoolsEmergSlots);
//  系统所有日志
    connect(this->actSyslog, &QAction::triggered, this, &Logtools::logtoolsSyslogSlots);
//  打印相关日志
    connect(this->actPrintRelated, &QAction::triggered, this, &Logtools::logtoolsPrintRelatedSlots);
//  关于菜单项
    connect(this->actAbout, &QAction::triggered, [=]() {
        logtoolsAboutLogtools();
    });
}

void Logtools::logtoolsGetHomePath()
{
    this->exportPath = GetContent::getHomePath() + "/桌面/";
}

void Logtools::logtoolsMessagesBox()
{
    QMessageBox box(QMessageBox::Information, "提示", "该日志文件为空或不存在！");
    box.setButtonText(QMessageBox::Ok, QString(" 确 定"));
    box.exec();
}

void Logtools::logtoolsSaveCurAlreadyTofindData(QList<QMap<QString, QString> > &logList)
{
    this->currentShowTable.curAlreadyTofindLogList.clear();
    this->currentShowTable.curAlreadyTofindLogList.append(logList);
    //    qDebug() << logList;
}

void Logtools::logtoolsSaveCurStateHandle(functionIndex enumTemp, void (*funcShowTable)(QList<QMap<QString, QString> > &, QTableView *, QStandardItemModel *))
{
    logtoolsCleanFilterCondition();
    logtoolsSaveCurrentEvent(enumTemp);
    logtoolsSaveCurrentComboBox();
    logtoolsSaveCurrentLogList();
    logtoolsSaveCurrentShowTableFunc(funcShowTable);
}

void Logtools::logtoolsProcessFilterEvent()
{
    logtoolsSaveCurrentComboBox();
#ifdef NEARLY_TIME
    logtoolsFilterNearlyChooseHandle();
#endif

    switch (this->currentShowTable.curEvent) {
    case enumSwitch:
#ifdef YEARMONTH_COMBOBOX
        logtoolsFilterSwitchGetData();
#elif defined (NEARLY_TIME)
        logtoolsFilterGeneralSearchGetData();
#endif
        ShowTable::logtoolsSwitchTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumSysInfo:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsSysInfoTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumSysBoot:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsSysBootTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumPackage:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsPackageTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumKernel:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsKernelTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumSoftwareUpdate:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsSoftwareUpdateTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumPrint:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsPrintTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumBurn:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsBurnTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumAudit:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsAuditTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumSession:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsSessionTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumXorg:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsXorgTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumApplication:
        logtoolsFilterApplicationGetData();
        ShowTable::logtoolsApplicationTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumSwitchTimes:
#ifdef YEARMONTH_COMBOBOX
        logtoolsFilterSwitchTimesGetData();
#elif defined (NEARLY_TIME)
        logtoolsFilterGeneralSearchGetData();
#endif
        ShowTable::logtoolsSwitchTimesTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumDebug:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsDebugTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumInfo:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsInfoTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumNotice:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsNoticeTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumWarning:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsWarningTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumError:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsErrorTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumCritical:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsCriticalTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumAlert:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsAlertTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    case enumEmerg:
        logtoolsFilterGeneralSearchGetData();
        ShowTable::logtoolsEmergTable(this->currentShowTable.curAlreadyTofindLogList, ui->tableView, this->model);
        break;
    default:
        qDebug() << "Event is not exist.";
        break;
    }
}

QList<QMap<QString, QString> > *Logtools::logtoolsEvent(functionIndex currentEvent)
{
    switch (currentEvent) {
    case enumSwitch:
        return &logListSwitch;
        break;
    case enumSysInfo:
        return &logListSysInfo;
        break;
    case enumSysBoot:
        return &logListSysBoot;
        break;
    case enumPackage:
        return &logListPackage;
        break;
    case enumKernel:
        return &logListKernel;
        break;
    case enumSoftwareUpdate:
        return &logListSoftwareUpdate;
        break;
    case enumPrint:
        return &logListPrint;
        break;
    case enumBurn:
        return &logListBurn;
        break;
    case enumAudit:
        return &logListAudit;
        break;
    case enumSession:
        return &logListSession;
        break;
    case enumXorg:
        return &logListXorg;
        break;
    case enumApplication:
        return &logListApplication;
        break;
    case enumSwitchTimes:
        return &logListSwitchTimes;
        break;
    case enumDebug:
        return &logListDebug;
        break;
    case enumInfo:
        return &logListInfo;
        break;
    case enumNotice:
        return &logListNotice;
        break;
    case enumWarning:
        return &logListWarning;
        break;
    case enumError:
        return &logListError;
        break;
    case enumCritical:
        return &logListCritical;
        break;
    case enumAlert:
        return &logListAlert;
        break;
    case enumEmerg:
        return &logListEmerg;
        break;
    default:
        qDebug() << "Event is not exist.";
        break;
    }
    return NULL;
}

void Logtools::logtoolsSetComboAppShow()
{
#ifdef YEARMONTH_COMBOBOX
    ui->comboBoxYear->hide();
    ui->comboBoxMonth->hide();
    ui->labelYear->hide();
    ui->labelMonth->hide();
#endif
    ui->comboBoxApp->show();
#ifdef NEARLY_TIME
    ui->labelInfo->hide();
    ui->pushButtonNearlyAll->show();
    ui->pushButtonNearlyToday->show();
    ui->pushButtonNearlyThreeDay->show();
    ui->pushButtonNearlyWeek->show();
    ui->pushButtonNearlyMonth->show();
    ui->pushButtonNearlyThreeMonth->show();
#endif
}

void Logtools::logtoolsSetFilterBarShow()
{
#ifdef YEARMONTH_COMBOBOX
    ui->comboBoxYear->show();
    ui->comboBoxMonth->show();
    ui->labelYear->show();
    ui->labelMonth->show();
#endif
    ui->comboBoxApp->hide();
#ifdef NEARLY_TIME
    ui->labelInfo->show();
    ui->pushButtonNearlyAll->show();
    ui->pushButtonNearlyToday->show();
    ui->pushButtonNearlyThreeDay->show();
    ui->pushButtonNearlyWeek->show();
    ui->pushButtonNearlyMonth->show();
    ui->pushButtonNearlyThreeMonth->show();
#endif
}

void Logtools::logtoolsSetFilterBarHide()
{
#ifdef YEARMONTH_COMBOBOX
    ui->comboBoxYear->hide();
    ui->comboBoxMonth->hide();
    ui->labelYear->hide();
    ui->labelMonth->hide();
#endif
    ui->comboBoxApp->hide();
#ifdef NEARLY_TIME
    ui->labelInfo->show();
    ui->pushButtonNearlyAll->hide();
    ui->pushButtonNearlyToday->hide();
    ui->pushButtonNearlyThreeDay->hide();
    ui->pushButtonNearlyWeek->hide();
    ui->pushButtonNearlyMonth->hide();
    ui->pushButtonNearlyThreeMonth->hide();
#endif
}

QString Logtools::logtoolsGetSearchLineEdit()
{
    return this->currentShowTable.curComboBoxSearch;
}

bool Logtools::logtoolsSearch(QMap<QString, QString> &alreadyToFind, QString &search)
{
    bool ret = false;
    for (int j=0; j<alreadyToFind.size(); j++) {
        if (alreadyToFind.values()[j].contains(search)) {
            ret = true;
            return ret;
        }
    }
    return ret;
}

void Logtools::logtoolsFilterGeneralSearchGetData()
{
    QString search = logtoolsGetSearchLineEdit();
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<this->currentShowTable.curLogListBack.size(); i++) {
        bool flag = false;
        if (!search.isEmpty()){
            flag = logtoolsSearch(this->currentShowTable.curLogListBack[i], search);
        }
        else {
            flag = true;
        }

        if (flag) {
            temp.append(this->currentShowTable.curLogListBack[i]);
        }
    }

    logtoolsSaveCurAlreadyTofindData(temp);
}

void Logtools::logtoolsFilterApplicationGetData()
{
    QString app = this->currentShowTable.curComboBoxApp;
    QString search = logtoolsGetSearchLineEdit();
    QString dataTemp;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<this->currentShowTable.curLogListBack.size(); i++) {
        dataTemp = this->currentShowTable.curLogListBack[i].value("来源");
        bool flag = false;
        bool appMatch = false;
        if (app.isEmpty()){
            appMatch = true;
        }
        else {
            if (dataTemp == app) {
                appMatch = true;
            }
        }
        bool searchMatch = logtoolsSearch(this->currentShowTable.curLogListBack[i], search);
        if (appMatch && searchMatch) {
            flag = true;
        }

        if (flag) {
            temp.append(this->currentShowTable.curLogListBack[i]);
        }
    }

    logtoolsSaveCurAlreadyTofindData(temp);
}


void Logtools::logtoolsFilterSetApplicationCombobox()
{
    ui->comboBoxApp->addItem("软件中心");
    ui->comboBoxApp->addItem("文件管理器");
    ui->comboBoxApp->addItem("方德压缩");
}

#ifdef YEARMONTH_COMBOBOX
void Logtools::logtoolsSaveCurrentComboBoxMap(QMap<QString, QStringList> comboBoxFilterMap)
{
    this->currentShowTable.curComboBoxFilterMap.clear();
    this->currentShowTable.curComboBoxFilterMap = comboBoxFilterMap;
//    qDebug() << comboBoxFilterMap;
}

void Logtools::logtoolsFilterSwitchGetData()
{
    QString year = this->currentShowTable.curComboBoxYear;
    QString month = this->currentShowTable.curComboBoxMonth;
    QString search = logtoolsGetSearchLineEdit();
    QList<QMap<QString, QString> > temp;
    QString regExpYearString = "^" + year;
    QString regExpMonthString = "^" + month + "$";
    QStringList dateTemp;
    temp.clear();

    for (int i=0; i<this->currentShowTable.curLogListBack.size(); i++) {
        dateTemp = this->currentShowTable.curLogListBack[i].value("日期").split("-");
        bool flag = false;
        bool yearMatch = dateTemp[0].contains(QRegExp(regExpYearString));
        bool monthMatch = true;
        if (month.isEmpty()) {
            monthMatch = true;
        }
        else {
            monthMatch = dateTemp[1].contains(QRegExp(regExpMonthString));
        }
        bool searchMatch = logtoolsSearch(this->currentShowTable.curLogListBack[i], search);

        if (yearMatch && monthMatch && searchMatch) {
            flag = true;
        }

        if (flag) {
            temp.append(this->currentShowTable.curLogListBack[i]);
        }
    }

    logtoolsSaveCurAlreadyTofindData(temp);
}

void Logtools::logtoolsFilterSwitchTimesGetData()
{
    QString month = this->currentShowTable.curComboBoxMonth;
    QString search = logtoolsGetSearchLineEdit();
    QList<QMap<QString, QString> > temp;
    QString regExpYearString = this->currentShowTable.curComboBoxYear;
    QString regExpMonthString = this->currentShowTable.curComboBoxMonth;
    QString yearTemp;
    QString monthTemp;
    temp.clear();

    for (int i=0; i<this->currentShowTable.curLogListBack.size(); i++) {
        yearTemp = this->currentShowTable.curLogListBack[i].value("年份");
        monthTemp = this->currentShowTable.curLogListBack[i].value("月份");
        bool flag = false;
        bool yearMatch = yearTemp.contains(QRegExp(regExpYearString));
        bool monthMatch = true;
        if (month.isEmpty()) {
            monthMatch = true;
        }
        else {
            monthMatch = monthTemp.contains(QRegExp(regExpMonthString));
        }

        bool searchMatch = logtoolsSearch(this->currentShowTable.curLogListBack[i], search);

        if (yearMatch && monthMatch && searchMatch) {
            flag = true;
        }

        if (flag) {
            temp.append(this->currentShowTable.curLogListBack[i]);
        }
    }

    logtoolsSaveCurAlreadyTofindData(temp);
}

void Logtools::logtoolsFilterSetSwitchCombobox(QList<QMap<QString, QString> > &logList)
{
    QStringList lineContent;
    QMap<QString, QStringList> comboBoxFilterMap;
    QMap<QString, QList<int> > filterSwitchMap;

    for (int i=0; i<logList.size(); i++) {
        lineContent.clear();
        lineContent = logList[i]["日期"].split("-");

        //重复月份不追加
        QList<int> month;
        int temp = lineContent[1].toInt();
        if (filterSwitchMap.size() < 1) {
            month.append(temp);
            filterSwitchMap.insert(lineContent[0], month);
            continue;
        }

        if (!filterSwitchMap[lineContent[0]].contains(temp)) {
            filterSwitchMap[lineContent[0]].append(temp);
        }
    }
//    qDebug() << filterSwitchMap;

    //将年份插入到comboBoxYear中,并将月份进行排序
    for (QMap<QString, QList<int>>::Iterator it=filterSwitchMap.begin(); it!=filterSwitchMap.end(); it++) {
        ui->comboBoxYear->addItem(it.key());
        sort(it.value().begin(), it.value().end());
    }

    //将QList<int>转化为QStringList
    for (QMap<QString, QList<int>>::Iterator it=filterSwitchMap.begin(); it!=filterSwitchMap.end(); it++) {
        QStringList temp;
        for (int i=0; i<it.value().size(); i++) {
            temp.append(QString::number(it.value().at(i)));
        }
        comboBoxFilterMap.insert(it.key(), temp);
    }
//    qDebug() << comboBoxFilterMap;
    logtoolsSaveCurrentComboBoxMap(comboBoxFilterMap);
}

void Logtools::logtoolsFilterSetSwitchTimesCombobox(QList<QMap<QString, QString> > &logList)
{
    QMap<QString, QStringList> comboBoxFilterMap;
    QMap<QString, QList<int> > filterSwitchMap;

    for (int i=0; i<logList.size(); i++) {
        //重复数据不追加
        QList<int> month;
        int temp = logList[i].value("月份").toInt();
        if (filterSwitchMap.size() < 1) {
            month.append(temp);
            filterSwitchMap.insert(logList[0].value("年份"), month);
            continue;
        }

        if (!filterSwitchMap[logList[i].value("年份")].contains(temp)) {
            filterSwitchMap[logList[i].value("年份")].append(temp);
        }
    }

    //将年份插入到comboBoxYear中,并将月份进行排序
    for (QMap<QString, QList<int>>::Iterator it=filterSwitchMap.begin(); it!=filterSwitchMap.end(); it++) {
        ui->comboBoxYear->addItem(it.key());
        sort(it.value().begin(), it.value().end());
    }

    //将QList<int>转化为QStringList
    for (QMap<QString, QList<int>>::Iterator it=filterSwitchMap.begin(); it!=filterSwitchMap.end(); it++) {
        QStringList temp;
        for (int i=0; i<it.value().size(); i++) {
            temp.append(QString::number(it.value().at(i)));
        }
        comboBoxFilterMap.insert(it.key(), temp);
    }

    logtoolsSaveCurrentComboBoxMap(comboBoxFilterMap);
}

#elif defined (NEARLY_TIME)

void Logtools::logtoolsFilterClearNearlyButtonStatus()
{
    ui->pushButtonNearlyAll->setChecked(true);
}

void Logtools::logtoolsFilterSetNearlyButtonStyle()
{
    ui->pushButtonNearlyAll->setFlat(true);
    ui->pushButtonNearlyToday->setFlat(true);
    ui->pushButtonNearlyThreeDay->setFlat(true);
    ui->pushButtonNearlyWeek->setFlat(true);
    ui->pushButtonNearlyMonth->setFlat(true);
    ui->pushButtonNearlyThreeMonth->setFlat(true);

    ui->pushButtonNearlyAll->setCheckable(true);
    ui->pushButtonNearlyToday->setCheckable(true);
    ui->pushButtonNearlyThreeDay->setCheckable(true);
    ui->pushButtonNearlyWeek->setCheckable(true);
    ui->pushButtonNearlyMonth->setCheckable(true);
    ui->pushButtonNearlyThreeMonth->setCheckable(true);

    ui->pushButtonNearlyAll->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                            QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
    ui->pushButtonNearlyToday->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                            QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
    ui->pushButtonNearlyThreeDay->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                            QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
    ui->pushButtonNearlyWeek->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                            QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
    ui->pushButtonNearlyMonth->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                            QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
    ui->pushButtonNearlyThreeMonth->setStyleSheet("QPushButton:hover {color:#3584E4;}\
                                            QPushButton:pressed {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}\
                                                  QPushButton:checked {background-color:#3584E4; border-radius:4px; color:#FFFFFF;}");
}

void Logtools::logtoolsFilterSetNearlyBxuttonGroup()
{
    this->buttonGroup = new QButtonGroup;
    this->buttonGroup->addButton(ui->pushButtonNearlyAll);
    this->buttonGroup->addButton(ui->pushButtonNearlyToday);
    this->buttonGroup->addButton(ui->pushButtonNearlyThreeDay);
    this->buttonGroup->addButton(ui->pushButtonNearlyWeek);
    this->buttonGroup->addButton(ui->pushButtonNearlyMonth);
    this->buttonGroup->addButton(ui->pushButtonNearlyThreeMonth);
    this->buttonGroup->setId(ui->pushButtonNearlyAll, nearlyAll);
    this->buttonGroup->setId(ui->pushButtonNearlyToday, nearlyToday);
    this->buttonGroup->setId(ui->pushButtonNearlyThreeDay, nearlyThreeDay);
    this->buttonGroup->setId(ui->pushButtonNearlyWeek, nearlyWeek);
    this->buttonGroup->setId(ui->pushButtonNearlyMonth, nearlyMonth);
    this->buttonGroup->setId(ui->pushButtonNearlyThreeMonth, nearlyThreeMonth);
    this->buttonGroup->setExclusive(true);
}

void Logtools::logtoolsFilterSaveSelectedNearlyButton(nearlyIndex id)
{
    this->currentShowTable.choose = id;
}

void Logtools::logtoolsFilterNearlyAllGetData()
{
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(*(this->currentShowTable.curLogListTempAddr));
}

void Logtools::logtoolsFilterNearlyTodayGetData()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString currentDate = currentDateTime.toString("M-dd");
    QString dataTemp;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<(*this->currentShowTable.curLogListTempAddr).size(); i++) {
        dataTemp = (*this->currentShowTable.curLogListTempAddr)[i].value("日期");

        if (dataTemp.contains(currentDate)) {
            temp.append((*this->currentShowTable.curLogListTempAddr)[i]);
        }
        else {
            break;
        }
    }
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(temp);
}

void Logtools::logtoolsFilterNearlyThreeDayGetData()
{
    #define DATATIMESIZE 4
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString   currentYear = currentDateTime.toString("yyyy");
    QString   dataTemp;
    QDateTime dataTempTime;
    QStringList timeTemp;
    int days;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<(*this->currentShowTable.curLogListTempAddr).size(); i++) {
        dataTemp = (*this->currentShowTable.curLogListTempAddr)[i].value("日期");
        if (dataTemp.size() == DATATIMESIZE) { //区分带年份和不带年份
            dataTemp = currentYear + "-" + dataTemp;
        }
        dataTempTime = QDateTime::fromString(dataTemp, "yyyy-M-dd");
        days = currentDateTime.daysTo(dataTempTime);
        if (days < NEARLYTHREEDAY) {
            break;
        }
        temp.append((*this->currentShowTable.curLogListTempAddr)[i]);
    }
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(temp);
}

void Logtools::logtoolsFilterNearlyWeekGetData()
{
    #define DATATIMESIZE 4
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString   currentYear = currentDateTime.toString("yyyy");
    QString   dataTemp;
    QDateTime dataTempTime;
    QStringList timeTemp;
    int days;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<(*this->currentShowTable.curLogListTempAddr).size(); i++) {
        dataTemp = (*this->currentShowTable.curLogListTempAddr)[i].value("日期");
        if (dataTemp.size() == DATATIMESIZE) { //区分带年份和不带年份
            dataTemp = currentYear + "-" + dataTemp;
        }
        dataTempTime = QDateTime::fromString(dataTemp, "yyyy-M-dd");
        days = currentDateTime.daysTo(dataTempTime);
        if (days < NEARLYWEEK) {
            break;
        }
        temp.append((*this->currentShowTable.curLogListTempAddr)[i]);
    }
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(temp);
}

void Logtools::logtoolsFilterNearlyMonthGetData()
{
    #define DATATIMESIZE 4
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString   currentYear = currentDateTime.toString("yyyy");
    QString   dataTemp;
    QDateTime dataTempTime;
    QStringList timeTemp;
    int days;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<(*this->currentShowTable.curLogListTempAddr).size(); i++) {
        dataTemp = (*this->currentShowTable.curLogListTempAddr)[i].value("日期");
        if (dataTemp.size() == DATATIMESIZE) { //区分带年份和不带年份
            dataTemp = currentYear + "-" + dataTemp;
        }
        dataTempTime = QDateTime::fromString(dataTemp, "yyyy-M-dd");
        days = currentDateTime.daysTo(dataTempTime);
        if (days < NEARLYMONTH) {
            break;
        }
        temp.append((*this->currentShowTable.curLogListTempAddr)[i]);
    }
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(temp);
}

void Logtools::logtoolsFilterNearlyThreeMonthGetData()
{
    #define DATATIMESIZE 4
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString   currentYear = currentDateTime.toString("yyyy");
    QString   dataTemp;
    QDateTime dataTempTime;
    QStringList timeTemp;
    int days;
    QList<QMap<QString, QString> > temp;
    temp.clear();

    for (int i=0; i<(*this->currentShowTable.curLogListTempAddr).size(); i++) {
        dataTemp = (*this->currentShowTable.curLogListTempAddr)[i].value("日期");
        if (dataTemp.size() == DATATIMESIZE) { //区分带年份和不带年份
            dataTemp = currentYear + "-" + dataTemp;
        }
        dataTempTime = QDateTime::fromString(dataTemp, "yyyy-M-dd");
        days = currentDateTime.daysTo(dataTempTime);
        if (days < NEARLYTHREEMONTH) {
            break;
        }
        temp.append((*this->currentShowTable.curLogListTempAddr)[i]);
    }
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(temp);
}

void Logtools::logtoolsFilterNearlyChooseHandle()
{
    switch (this->currentShowTable.choose) {
    case nearlyAll:
        logtoolsFilterNearlyAllGetData();
        break;
    case nearlyToday:
        logtoolsFilterNearlyTodayGetData();
        break;
    case nearlyThreeDay:
        logtoolsFilterNearlyThreeDayGetData();
        break;
    case nearlyWeek:
        logtoolsFilterNearlyWeekGetData();
        break;
    case nearlyMonth:
        logtoolsFilterNearlyMonthGetData();
        break;
    case nearlyThreeMonth:
        logtoolsFilterNearlyThreeMonthGetData();
        break;
    default:
        qDebug() << "Choose is not exist.";
        break;
    }
}
#endif

void Logtools::logtoolsLoadingAllLogList()
{
    if (!this->logListSwitch.size()) {
        ParserLog::parserSwitch(logListSwitch);
    }
    if (!this->logListSysInfo.size()) {
        ParserLog::parserSysInfo(logListSysInfo);
    }
    if (!this->logListSysBoot.size()) {
        ParserLog::parserSysBoot(logListSysBoot);
    }
    if (!this->logListPackage.size()) {
        ParserLog::parserPackage(logListPackage);
    }
    if (!this->logListKernel.size()) {
        ParserLog::parserKernel(logListKernel);
    }
    if (!this->logListSoftwareUpdate.size()) {
        ParserLog::parserSoftwareUpdate(logListSoftwareUpdate);
    }
    if (!this->logListPrint.size()) {
        ParserLog::parserPrint(logListPrint);
    }
    if (!this->logListBurn.size()) {
        ParserLog::parserBurn(logListBurn);
    }
    if (!this->logListAudit.size()) {
        ParserLog::parserAudit(logListAudit);
    }
    if (!this->logListSession.size()) {
        ParserLog::parserSession(logListSession);
    }
    if (!this->logListXorg.size()) {
        ParserLog::parserXorg(logListXorg);
    }
    if (!this->logListApplication.size()) {
        ParserLog::parserApplication(logListApplication);
    }
    if (!this->logListSwitchTimes.size()) {
        ParserLog::parserSwitchTimes(logListSwitchTimes);
    }
    if (!this->logListDebug.size()) {
        ParserLog::parserSwitchTimes(logListDebug);
    }
    if (!this->logListInfo.size()) {
        ParserLog::parserSwitchTimes(logListInfo);
    }
    if (!this->logListNotice.size()) {
        ParserLog::parserSwitchTimes(logListNotice);
    }
    if (!this->logListWarning.size()) {
        ParserLog::parserSwitchTimes(logListWarning);
    }
    if (!this->logListError.size()) {
        ParserLog::parserSwitchTimes(logListError);
    }
    if (!this->logListCritical.size()) {
        ParserLog::parserSwitchTimes(logListCritical);
    }
    if (!this->logListAlert.size()) {
        ParserLog::parserSwitchTimes(logListAlert);
    }
    if (!this->logListEmerg.size()) {
        ParserLog::parserSwitchTimes(logListEmerg);
    }
}

void Logtools::logtoolsChangeChmod(QString &file)
{
    QProcess *execCmd = new QProcess();
    QStringList params;
    //执行修改文件的权限
    params.clear();
    params.append("666");
    params.append(file);
    execCmd->start("chmod", params);
    execCmd->waitForFinished();
    execCmd->close();
}

void Logtools::logtoolsExportSingleLogList(QString path, QString fileName, QList<QMap<QString, QString> > &logList)
{
    QString logFile = path + fileName;

    QFile file(logFile);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "Open " << logFile << " error";
        return;
    }

    for (int i=0; i<logList.size(); i++) {
        for (int j=0; j<logList[i].values().size(); j++) {
            QString tempStr = logList[i].values()[j] + "  ";
            file.write(tempStr.toUtf8());
        }
        file.write("\n");
    }
    file.close();
}

void Logtools::exportZip(QString &srcDir, QString &dstDir, bool flag)
{
    QProcess execCmd;
    QStringList params;
    QString exportFile = "";
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString currentDate = currentDateTime.toString("yyyy.MM.dd-hh:mm:ss");
    exportFile = dstDir + "_" + currentDate + ".zip"; //format eg: cups_2022.08.16-14:36:05.zip
//    qDebug() << exportFile;

    //执行压缩命令
    params.append("-q");
    params.append("-r");
    if (true == flag) {
        params.append("-j");
    }
    params.append(exportFile);
    params.append(srcDir);
    execCmd.start("zip", params);
    execCmd.waitForFinished();
    execCmd.close();

    logtoolsChangeChmod(exportFile);
}

void Logtools::logtoolsGifDialogStart()
{
    this->loadingGif = new QDialog(this);
    this->loadingGif->setFixedSize(100, 100);
    this->loadingGif->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    this->loadingGif->setAttribute(Qt::WA_NoBackground);
    this->loadingGif->setAttribute(Qt::WA_TranslucentBackground);

    this->loadingLable = new QLabel(this->loadingGif);
    this->loadingLable->setFixedSize(100, 100);

    this->loadingMovie = new QMovie(":/icon/nfs-logtools_loading.gif");
    this->loadingMovie->setScaledSize(QSize(100, 100));
    this->loadingLable->setMovie(this->loadingMovie);
    this->loadingMovie->start();

    this->loadingGif->exec();
}

void Logtools::logtoolsGifDialogStop()
{
    this->loadingMovie->stop();
    this->loadingGif->close();
}

void Logtools::logtoolsCleanFilterCondition()
{
#ifdef YEARMONTH_COMBOBOX
    ui->comboBoxYear->clear();
    ui->comboBoxMonth->clear();
    ui->comboBoxYear->addItem("所有");
    ui->comboBoxMonth->addItem("所有");
#endif
    ui->lineEditSearch->clear();
    ui->comboBoxApp->clear();
    ui->comboBoxApp->addItem("所有应用");
#ifdef NEARLY_TIME
    logtoolsFilterClearNearlyButtonStatus();
    this->currentShowTable.choose = nearlyAll;
#endif
}

void Logtools::logtoolsSaveCurrentEvent(functionIndex currentEvent)
{
    this->currentShowTable.curEvent = currentEvent;
}

void Logtools::logtoolsSaveCurrentComboBox()
{
    this->currentShowTable.curComboBoxSearch = ui->lineEditSearch->text();
    this->currentShowTable.curComboBoxApp    = ui->comboBoxApp->currentText();
    if ("所有应用" == this->currentShowTable.curComboBoxApp) {
        this->currentShowTable.curComboBoxApp = "";
    }
#ifdef YEARMONTH_COMBOBOX
    this->currentShowTable.curComboBoxYear   = ui->comboBoxYear->currentText();
    this->currentShowTable.curComboBoxMonth  = ui->comboBoxMonth->currentText();
    if ("所有" == this->currentShowTable.curComboBoxYear) {
        this->currentShowTable.curComboBoxYear = "";
    }
    if ("所有" == this->currentShowTable.curComboBoxMonth) {
        this->currentShowTable.curComboBoxMonth = "";
    }
#endif
}

void Logtools::logtoolsSaveCurrentLogList()
{
    this->currentShowTable.curLogListTempAddr = this->logtoolsEvent(this->currentShowTable.curEvent);
    this->currentShowTable.curLogListBack.clear();
    this->currentShowTable.curLogListBack.append(*(this->currentShowTable.curLogListTempAddr));
    logtoolsSaveCurAlreadyTofindData(this->currentShowTable.curLogListBack);
}

void Logtools::logtoolsSaveCurrentShowTableFunc(void (*funcShowTable)(QList<QMap<QString, QString> > &, QTableView *, QStandardItemModel *))
{
    this->currentShowTable.funcShowTable = funcShowTable;
}

void Logtools::logtoolsSwitchSlots()
{
//    qDebug() << "开关机与登录情况";
    //判断变量是否有数据
    if (!this->logListSwitch.size()) {
        ParserLog::parserSwitch(logListSwitch);
    }

    //文件存在且不为空进入if
    if ((fileExist && !fileEmpty) || this->logListSwitch.size()) {
        logtoolsSaveCurStateHandle(enumSwitch, ShowTable::logtoolsSwitchTable);
#ifdef YEARMONTH_COMBOBOX
        logtoolsFilterSetSwitchCombobox(this->currentShowTable.curLogListBack);
#endif
        ShowTable::logtoolsSwitchTable(this->logListSwitch, ui->tableView, this->model);
        ui->labelInfo->setText(QString("开关机与登录情况："));
        logtoolsSetFilterBarShow();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSysInfoSlots()
{
//    qDebug() << "系统相关信息";
    //判断变量是否有数据
    if (!this->logListSysInfo.size()) {
        ParserLog::parserSysInfo(logListSysInfo);
    }

    //文件存在且不为空进入if
    if ((fileExist && !fileEmpty) || this->logListSysInfo.size()) {
        logtoolsSaveCurStateHandle(enumSysInfo, ShowTable::logtoolsSysInfoTable);
        ShowTable::logtoolsSysInfoTable(this->logListSysInfo, ui->tableView, this->model);
        ui->labelInfo->setText(QString("系统相关信息："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSysBootSlots()
{
//    qDebug() << "系统启动信息";
    if (!this->logListSysBoot.size()) {
        ParserLog::parserSysBoot(logListSysBoot);
    }

    //文件存在且不为空进入if
    if ((fileExist && !fileEmpty) || this->logListSysBoot.size()) {
        logtoolsSaveCurStateHandle(enumSysBoot, ShowTable::logtoolsSysBootTable);
        ShowTable::logtoolsSysBootTable(this->logListSysBoot, ui->tableView, this->model);
        ui->labelInfo->setText(QString("系统启动信息："));
        this->logtoolsSetFilterBarHide();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsPackageSlots()
{
//    qDebug() << "包安装情况";
    if (!this->logListPackage.size()) {
        ParserLog::parserPackage(logListPackage);
    }

    //文件存在且不为空进入if
    if ((fileExist && !fileEmpty) || this->logListPackage.size()) {
        logtoolsSaveCurStateHandle(enumPackage, ShowTable::logtoolsPackageTable);
        ShowTable::logtoolsPackageTable(this->logListPackage, ui->tableView, this->model);
        ui->labelInfo->setText(QString("包安装情况："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
    this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsKernelSlots()
{
//    qDebug() << "内核信息";
    if (!this->logListKernel.size()) {
        ParserLog::parserKernel(logListKernel);
    }

    if ((fileExist && !fileEmpty) || this->logListKernel.size()) {
        logtoolsSaveCurStateHandle(enumKernel, ShowTable::logtoolsKernelTable);
        ShowTable::logtoolsKernelTable(this->logListKernel, ui->tableView, this->model);
        ui->labelInfo->setText(QString("内核信息："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
    this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSoftwareUpdateSlots()
{
//    qDebug() << "软件更新信息";
    if (!this->logListSoftwareUpdate.size()) {
        ParserLog::parserSoftwareUpdate(logListSoftwareUpdate);
    }

    if ((fileExist && !fileEmpty) || this->logListSoftwareUpdate.size()) {
        logtoolsSaveCurStateHandle(enumSoftwareUpdate, ShowTable::logtoolsSoftwareUpdateTable);
        ShowTable::logtoolsSoftwareUpdateTable(this->logListSoftwareUpdate, ui->tableView, this->model);
        ui->labelInfo->setText(QString("软件更新信息："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
    this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsPrintSlots()
{
//    qDebug() << "打印信息";
    if (!this->logListPrint.size()) {
        ParserLog::parserPrint(logListPrint);
    }

    if ((fileExist && !fileEmpty) || this->logListPrint.size()) {
        logtoolsSaveCurStateHandle(enumPrint, ShowTable::logtoolsPrintTable);
        ShowTable::logtoolsPrintTable(this->logListPrint, ui->tableView, this->model);
        ui->labelInfo->setText(QString("打印信息："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
    this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsBurnSlots()
{
//    qDebug() << "刻录日志";
    if (!this->logListBurn.size()) {
        ParserLog::parserBurn(logListBurn);
    }

    if ((fileExist && !fileEmpty) || this->logListBurn.size()) {
        logtoolsSaveCurStateHandle(enumBurn, ShowTable::logtoolsBurnTable);
        ShowTable::logtoolsBurnTable(this->logListBurn, ui->tableView, this->model);
        ui->labelInfo->setText(QString("刻录日志："));
        this->logtoolsSetFilterBarHide();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsAuditSlots()
{
//    qDebug() << "审计日志";
    if (!this->logListAudit.size()) {
        ParserLog::parserAudit(logListAudit);
    }

    if ((fileExist && !fileEmpty) || this->logListAudit.size()) {
        logtoolsSaveCurStateHandle(enumAudit, ShowTable::logtoolsAuditTable);
        ShowTable::logtoolsAuditTable(this->logListAudit, ui->tableView, this->model);
        ui->labelInfo->setText(QString("审计日志："));
        this->logtoolsSetFilterBarHide();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSessionSlots()
{
//    qDebug() << "会话日志";
    if (!this->logListSession.size()) {
        ParserLog::parserSession(logListSession);
    }

    if ((fileExist && !fileEmpty) || this->logListSession.size()) {
        logtoolsSaveCurStateHandle(enumSession, ShowTable::logtoolsSessionTable);
        ShowTable::logtoolsSessionTable(this->logListSession, ui->tableView, this->model);
        ui->labelInfo->setText(QString("会话日志："));
        this->logtoolsSetFilterBarHide();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsXorgSlots()
{
//    qDebug() << "Xorg信息";
    if (!this->logListXorg.size()) {
        ParserLog::parserXorg(logListXorg);
    }

    if ((fileExist && !fileEmpty) || this->logListXorg.size()) {
        logtoolsSaveCurStateHandle(enumXorg, ShowTable::logtoolsXorgTable);
        ShowTable::logtoolsXorgTable(this->logListXorg, ui->tableView, this->model);
        ui->labelInfo->setText(QString("Xorg信息："));
        this->logtoolsSetFilterBarHide();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsApplicationSlots()
{
//    qDebug() << "应用日志";
    if (!this->logListApplication.size()) {
        ParserLog::parserApplication(logListApplication);
    }

    if ((fileExist && !fileEmpty) || this->logListApplication.size()) {
        logtoolsSaveCurStateHandle(enumApplication, ShowTable::logtoolsApplicationTable);
        logtoolsFilterSetApplicationCombobox();
        ShowTable::logtoolsApplicationTable(this->logListApplication, ui->tableView, this->model);
        ui->labelInfo->setText(QString("应用列表："));
        this->logtoolsSetComboAppShow();
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSwitchTimesSlots()
{
//    qDebug() << "开关机次数统计";
    if (!this->logListSwitchTimes.size()) {
        ParserLog::parserSwitchTimes(logListSwitchTimes);
    }

    if ((fileExist && !fileEmpty) || this->logListSwitchTimes.size()) {
        logtoolsSaveCurStateHandle(enumSwitchTimes, ShowTable::logtoolsSwitchTimesTable);
#ifdef YEARMONTH_COMBOBOX
        logtoolsFilterSetSwitchTimesCombobox(this->currentShowTable.curLogListBack);
#endif
        ShowTable::logtoolsSwitchTimesTable(this->logListSwitchTimes, ui->tableView, this->model);
#ifdef NEARLY_TIME
        this->logtoolsSetFilterBarHide();
        ui->labelInfo->show();
#endif
        ui->labelInfo->setText(QString("开关机次数统计："));
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsDebugSlots()
{
//    qDebug() << "调试级别";
    if (!this->logListDebug.size()) {
        ParserLog::parserDebug(logListDebug);
    }

    if ((fileExist && !fileEmpty) || this->logListDebug.size()) {
        logtoolsSaveCurStateHandle(enumDebug, ShowTable::logtoolsDebugTable);
        ShowTable::logtoolsDebugTable(this->logListDebug, ui->tableView, this->model);
        ui->labelInfo->setText(QString("调试级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
    this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsInfoSlots()
{
//    qDebug() << "通知级别";
    if (!this->logListInfo.size()) {
        ParserLog::parserInfo(logListInfo);
    }

    if ((fileExist && !fileEmpty) || this->logListInfo.size()) {
        logtoolsSaveCurStateHandle(enumInfo, ShowTable::logtoolsInfoTable);
        ShowTable::logtoolsInfoTable(this->logListInfo, ui->tableView, this->model);
        ui->labelInfo->setText(QString("通知级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsNoticeSlots()
{
//    qDebug() << "注意级别";
    if (!this->logListNotice.size()) {
        ParserLog::parserNotice(logListNotice);
    }

    if ((fileExist && !fileEmpty) || this->logListNotice.size()) {
        logtoolsSaveCurStateHandle(enumNotice, ShowTable::logtoolsNoticeTable);
        ShowTable::logtoolsNoticeTable(this->logListNotice, ui->tableView, this->model);
        ui->labelInfo->setText(QString("注意级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsWarningSlots()
{
//    qDebug() << "警告级别";
    if (!this->logListWarning.size()) {
        ParserLog::parserWarning(logListWarning);
    }

    if ((fileExist && !fileEmpty) || this->logListWarning.size()) {
        logtoolsSaveCurStateHandle(enumWarning, ShowTable::logtoolsWarningTable);
        ShowTable::logtoolsWarningTable(this->logListWarning, ui->tableView, this->model);
        ui->labelInfo->setText(QString("警告级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsErrorSlots()
{
//    qDebug() << "错误级别";
    if (!this->logListError.size()) {
        ParserLog::parserError(this->logListError);
    }

    if ((fileExist && !fileEmpty) || this->logListError.size()) {
        logtoolsSaveCurStateHandle(enumError, ShowTable::logtoolsErrorTable);
        ShowTable::logtoolsErrorTable(this->logListError, ui->tableView, this->model);
        ui->labelInfo->setText(QString("错误级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsCriticalSlots()
{
//    qDebug() << "临界级别";
    if (!this->logListCritical.size()) {
        ParserLog::parserCritical(this->logListCritical);
    }

    if ((fileExist && !fileEmpty) || this->logListCritical.size()) {
        logtoolsSaveCurStateHandle(enumCritical, ShowTable::logtoolsCriticalTable);
        ShowTable::logtoolsCriticalTable(this->logListCritical, ui->tableView, this->model);
        ui->labelInfo->setText(QString("临界级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsAlertSlots()
{
//    qDebug() << "警戒级别";
    if (!this->logListAlert.size()) {
        ParserLog::parserAlert(this->logListAlert);
    }

    if ((fileExist && !fileEmpty) || this->logListAlert.size()) {
        logtoolsSaveCurStateHandle(enumAlert, ShowTable::logtoolsAlertTable);
        ShowTable::logtoolsAlertTable(this->logListAlert, ui->tableView, this->model);
        ui->labelInfo->setText(QString("警戒级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsEmergSlots()
{
//    qDebug() << "致命级别";
    if (!this->logListEmerg.size()) {
        ParserLog::parserEmerg(this->logListEmerg);
    }

    if ((fileExist && !fileEmpty) || this->logListEmerg.size()) {
        logtoolsSaveCurStateHandle(enumEmerg, ShowTable::logtoolsEmergTable);
        ShowTable::logtoolsEmergTable(this->logListEmerg, ui->tableView, this->model);
        ui->labelInfo->setText(QString("致命级别："));
#ifdef YEARMONTH_COMBOBOX
        this->logtoolsSetFilterBarHide();
#elif defined (NEARLY_TIME)
        this->logtoolsSetFilterBarShow();
#endif
    }
    else {
        logtoolsMessagesBox();
    }
}

void Logtools::logtoolsSyslogSlots()
{
    QString srcDir  = "/var/templog";
    QString logDir  = "/var/log";
    QString crashDir  = "/var/crash";
    QString dstDir = this->exportPath + "/system-all-log";
    GetContent checkDir;
    QString messageBoxText = "系统日志将导出到" + this->exportPath + ",确认导出吗？";

    QMessageBox *exportCupsLog = new QMessageBox;
    exportCupsLog->setWindowTitle(" ");
    exportCupsLog->setText(messageBoxText);
    exportCupsLog->addButton("确认", QMessageBox::AcceptRole);
    exportCupsLog->addButton("取消", QMessageBox::NoRole);

    int id = exportCupsLog->exec();
    if (0 == id) { //确认
        QProcess execCmd;
        QStringList params;
        params.append(srcDir);
        execCmd.start("mkdir", params);
        execCmd.waitForFinished();
        execCmd.close();
        params.clear();
        params.append("-r");
        params.append(logDir);
        params.append(crashDir);
        params.append(srcDir);
        execCmd.start("cp", params);
        execCmd.waitForFinished();
        execCmd.close();
        this->exportZip(srcDir, dstDir, false);
        params.clear();
        params.append("-r");
        params.append(srcDir);
        execCmd.start("rm", params);
        execCmd.waitForFinished();
        execCmd.close();
    }
    else {
        qDebug() << "取消";
    }
}

void Logtools::logtoolsPrintRelatedSlots()
{
    QString srcDir  = "/var/log/cups";
    QString dstDir = this->exportPath + "/cups";
    GetContent checkDir;
    QString messageBoxText = "打印日志将导出到" + this->exportPath + "，确认导出吗？";

    if (!checkDir.checkDirPathExists(srcDir)) {
        return ;
    }

    QMessageBox *exportCupsLog = new QMessageBox;
    exportCupsLog->setWindowTitle(" ");
    exportCupsLog->setText(messageBoxText);
    exportCupsLog->addButton("确认", QMessageBox::AcceptRole);
    exportCupsLog->addButton("取消", QMessageBox::NoRole);

    int id = exportCupsLog->exec();
    if (0 == id) { //确认
        this->exportZip(srcDir, dstDir, true);
    }
    else {
        qDebug() <<"取消";
    }
}

#ifdef YEARMONTH_COMBOBOX
void Logtools::on_ComboboxYear_Changed(int index)
{
    QString yearText = "";

    yearText = ui->comboBoxYear->itemText(index);
    //年份每改变一次就将所对应的月份链表更新一下
    ui->comboBoxMonth->clear();
    ui->comboBoxMonth->addItem("所有");
    ui->comboBoxMonth->addItems(this->currentShowTable.curComboBoxFilterMap.value(yearText));
    logtoolsProcessFilterEvent();
}

void Logtools::on_ComboboxMonth_Changed(int index)
{
    QString monthText;
    monthText = ui->comboBoxMonth->itemText(index);
    logtoolsProcessFilterEvent();
}
#elif defined (NEARLY_TIME)
void Logtools::on_pushButtonNearlyAll_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyAll);
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonNearlyToday_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyToday);
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonNearlyThreeDay_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyThreeDay);
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonNearlyWeek_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyWeek);
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonNearlyMonth_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyMonth);
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonNearlyThreeMonth_clicked()
{
    logtoolsFilterSaveSelectedNearlyButton(nearlyThreeMonth);
    logtoolsProcessFilterEvent();
}
#endif

void Logtools::on_buttonSearch_clicked()
{
    logtoolsProcessFilterEvent();
}

void Logtools::on_comboBoxApp_activated(const QString &arg)
{
    this->currentShowTable.curComboBoxApp = arg;
    logtoolsProcessFilterEvent();
}

void Logtools::on_pushButtonExportCur_clicked()
{
    QString exportFile = "";
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString currentDate = currentDateTime.toString("yyyy.MM.dd-hh:mm:ss");
    exportFile = "log_" + currentDate; //format eg: log_2022.08.16-14:36:05
    QString logFile = this->exportPath + exportFile;
    QString messageBoxText = "当前日志将导出到" + this->exportPath + "，确认导出吗？";
    QMessageBox *exportCurLog = new QMessageBox;

    exportCurLog->setWindowTitle(" ");
    exportCurLog->setText(messageBoxText);
    exportCurLog->addButton("确认", QMessageBox::AcceptRole);
    exportCurLog->addButton("取消", QMessageBox::NoRole);

    int id = exportCurLog->exec();
    if (0 == id) { //确认
        QFile file(logFile);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            qDebug() << "Open " << logFile << " error";
            return;
        }

        for (int i=0; i<this->currentShowTable.curAlreadyTofindLogList.size(); i++) {
            for (int j=0; j<this->currentShowTable.curAlreadyTofindLogList[i].values().size(); j++) {
                QString tempStr = this->currentShowTable.curAlreadyTofindLogList[i].values()[j] + "  ";
                file.write(tempStr.toUtf8());
            }
            file.write("\n");
        }
        file.close();

        logtoolsChangeChmod(logFile);
    }
    else {
        qDebug() << "取消";
    }
}

void Logtools::on_pushButtonExportAll_clicked()
{
    QString path = "/tmp/export/";
    QString dstFile = "";
    QString folderNmae = "export";
    QDir dir(QDir::tempPath());
    GetContent checkDir;
    QString messageBoxText = "系统日志将导出到" + this->exportPath + "，确认导出吗？";
    QMessageBox *exportLog = new QMessageBox;
    exportLog->setWindowTitle(" ");
    exportLog->setText(messageBoxText);
    exportLog->addButton("确认", QMessageBox::AcceptRole);
    exportLog->addButton("取消", QMessageBox::NoRole);
    int id = exportLog->exec();
    if (0 == id) { //确认
        logtoolsLoadingAllLogList();
        if (!dir.exists(folderNmae)) {
            dir.mkdir(folderNmae);
        }
        else {
            dir.rmdir(folderNmae);
            dir.mkdir(folderNmae);
        }

        logtoolsExportSingleLogList(path, "wtmp.log", this->logListSwitch);
        logtoolsExportSingleLogList(path, "sys.log", this->logListSysInfo);
        logtoolsExportSingleLogList(path, "boot.log", this->logListSysBoot);
        logtoolsExportSingleLogList(path, "dpkg.log", this->logListPackage);
        logtoolsExportSingleLogList(path, "kern.log", this->logListKernel);
        logtoolsExportSingleLogList(path, "alternatives.log", this->logListAlert);
        logtoolsExportSingleLogList(path, "cups.log", this->logListPrint);
        logtoolsExportSingleLogList(path, "brosera.log", this->logListBurn);
        logtoolsExportSingleLogList(path, "audit.log", this->logListAudit);
        logtoolsExportSingleLogList(path, "xsession.log", this->logListSession);
        logtoolsExportSingleLogList(path, "Xorg.log", this->logListXorg);
        logtoolsExportSingleLogList(path, "application.log", this->logListApplication);
        logtoolsExportSingleLogList(path, "switchtimes.log", this->logListSwitchTimes);
        logtoolsExportSingleLogList(path, "debug.log", this->logListDebug);
        logtoolsExportSingleLogList(path, "info.log", this->logListInfo);
        logtoolsExportSingleLogList(path, "notice.log", this->logListNotice);
        logtoolsExportSingleLogList(path, "warning.log", this->logListWarning);
        logtoolsExportSingleLogList(path, "error.log", this->logListError);
        logtoolsExportSingleLogList(path, "critical.log", this->logListCritical);
        logtoolsExportSingleLogList(path, "alert.log", this->logListAlert);
        logtoolsExportSingleLogList(path, "emerg.log", this->logListEmerg);
        dstFile = this->exportPath + "log";
        this->exportZip(path, dstFile, true);
    }
    else {
        qDebug() <<"取消";
    }
}

void Logtools::on_pushButtonUpdate_clicked()
{
    logtoolsEvent(this->currentShowTable.curEvent)->clear();

    switch (this->currentShowTable.curEvent) {
    case enumSwitch:
        logtoolsSwitchSlots();
        break;
    case enumSysInfo:
        logtoolsSysInfoSlots();
        break;
    case enumSysBoot:
        logtoolsSysBootSlots();
        break;
    case enumPackage:
        logtoolsPackageSlots();
        break;
    case enumKernel:
        logtoolsKernelSlots();
        break;
    case enumSoftwareUpdate:
        logtoolsSoftwareUpdateSlots();
        break;
    case enumPrint:
        logtoolsPrintSlots();
        break;
    case enumBurn:
        logtoolsBurnSlots();
        break;
    case enumAudit:
        logtoolsAuditSlots();
        break;
    case enumSession:
        logtoolsSessionSlots();
        break;
    case enumXorg:
        logtoolsXorgSlots();
        break;
    case enumApplication:
        logtoolsApplicationSlots();
        break;
    case enumSwitchTimes:
        logtoolsSwitchTimesSlots();
        break;
    case enumDebug:
        logtoolsDebugSlots();
        break;
    case enumInfo:
        logtoolsInfoSlots();
        break;
    case enumNotice:
        logtoolsNoticeSlots();
        break;
    case enumWarning:
        logtoolsWarningSlots();
        break;
    case enumError:
        logtoolsErrorSlots();
        break;
    case enumCritical:
        logtoolsCriticalSlots();
        break;
    case enumAlert:
        logtoolsAlertSlots();
        break;
    case enumEmerg:
        logtoolsEmergSlots();
        break;
    default:
        qDebug() << "Event is not exist.";
        break;
    }
}

void Logtools::on_pushButtonQuit_clicked()
{
    this->close();
}
